<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">{{$t('ads.home.payadsense')}}</view>
				<view class="action">
					<text class="text-blue" @tap="toLink('/pages/ads/myAds')">{{$t('ads.home.myadsense')}}</text>
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="ads-list">
			<view class="ads-box">
				<view class="ads-main">
					<view class="ads-name">{{$t('global.postpushadvert')}}</view>
					<view class="ads-text">
						{{$t('ads.home.postpushadvtxt')}}<text class="text-red">{{$t('ads.home.postpushadveffect')}}</text>
					</view>
					<view class="ads-info grid col-3 text-center">
						<view class="ads-num">
							{{$t('ads.home.remaining')}}:<text class="text-blue">{{pushAdsNum || 0}}</text>
						</view>
						<view class="ads-price">
							<text class="text-red">{{pushAdsPrice || 0}}{{currencyName}}</text>{{$t('ads.home.pointday')}}
						</view>
						<view class="ads-btn" @tap="goAdsBuy(pushAdsPrice,pushAdsNum,0)">
							<text class="text-green">{{$t('global.buynow')}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="ads-box">
				<view class="ads-main">
					<view class="ads-name">
						{{$t('global.banneradvert')}}
					</view>
					<view class="ads-text">
						{{$t('ads.home.banneradvtxt')}}<text class="text-red">{{$t('ads.home.banneradveffect')}}</text>
					</view>
					<view class="ads-info grid col-3 text-center">
						<view class="ads-num">
							{{$t('ads.home.remaining')}}:<text class="text-blue">{{bannerAdsNum || 0}}</text>
						</view>
						<view class="ads-price">
							<text class="text-red">{{bannerAdsPrice || 0}}{{currencyName}}</text>{{$t('ads.home.pointday')}}
						</view>
						<view class="ads-btn" @tap="goAdsBuy(bannerAdsPrice,bannerAdsNum,1)">
							<text class="text-green">{{$t('global.buynow')}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="ads-box">
				<view class="ads-main">
					<view class="ads-name">
						{{$t('global.statupadvert')}}
					</view>
					<view class="ads-text">
						{{$t('ads.home.statupadvtxt')}}<text class="text-red">{{$t('ads.home.statupadveffect')}}</text>
					</view>
					<view class="ads-info grid col-3 text-center">
						<view class="ads-num">
							{{$t('ads.home.remaining')}}:<text class="text-blue">{{startAdsNum || 0}}</text>
						</view>
						<view class="ads-price">
							<text class="text-red">{{startAdsPrice || 0}}{{currencyName}}</text>{{$t('ads.home.pointday')}}
						</view>
						<view class="ads-btn" @tap="goAdsBuy(startAdsPrice,startAdsNum,2)">
							<text class="text-green">{{$t('global.buynow')}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!--加载遮罩-->
		<view class="loading" v-if="isLoading==0">
			<view class="loading-main">
				<image src="../../static/loading.gif"></image>
			</view>
		</view>
		<!--加载遮罩结束-->
	</view>
</template>

<script>
	import {
		localStorage
	} from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar: this.StatusBar + this.CustomBar,
				AppStyle: this.$store.state.AppStyle,

				isLoading: 0,

				pushAdsNum: 0,
				pushAdsPrice: 0,
				bannerAdsNum: 0,
				bannerAdsPrice: 0,
				startAdsNum: 0,
				startAdsPrice: 0,

				token: "",
				userData: {
					assets: 0,
				},

				currencyName: "",
			}
		},
		onPullDownRefresh() {

		},
		onShow() {
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if (localStorage.getItem('token')) {
				that.token = localStorage.getItem('token');
			}
			that.getAdsConfig();
			that.getUserData();
		},
		mounted() {
			this.getleiji()
		},
		methods: {
			getleiji() {
				var that = this;
				uni.request({
					url: that.$API.SPset(),
					method: 'GET',
					dataType: "json",
					success(res) {
						that.currencyName = res.data.assetsname;
					},
					fail(error) {
						
					}
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			},
			getUserData() {
				var that = this;
				that.$Net.request({
					url: that.$API.getUserData(),
					data: {
						"token": that.token
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						if (res.data.code == 1) {
							that.userData = res.data.data;
						}
					},
					fail: function(res) {
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
			getAdsConfig() {
				var that = this;
				that.$Net.request({
					url: that.$API.adsConfig(),
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						if (res.data.code == 1) {
							that.pushAdsNum = res.data.data.pushAdsNum;
							that.pushAdsPrice = res.data.data.pushAdsPrice;
							that.bannerAdsNum = res.data.data.bannerAdsNum;
							that.bannerAdsPrice = res.data.data.bannerAdsPrice;
							that.startAdsNum = res.data.data.startAdsNum;
							that.startAdsPrice = res.data.data.startAdsPrice;
						}
						var timer = setTimeout(function() {
							that.isLoading = 1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						var timer = setTimeout(function() {
							that.isLoading = 1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			toLink(text) {
				var that = this;
				if(!localStorage.getItem('token')){
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: text
				});
			},
			goAdsBuy(price, num, type) {
				var that = this;
				if (num <= 0 || !num) {
					uni.showToast({
						title: that.$t('ads.home.advtxtsoldout'),
						icon: 'none'
					})
					return false;
				}
				if(!localStorage.getItem('token')){
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				var assets = that.userData.assets;
				if (price > assets) {
					uni.showToast({
						title: that.$t('ads.home.plsrechargetxt'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/ads/adsPost?post=add&type=' + type
				});
			},
		},
	}
</script>